import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import tcpunit.HttpClient;
import tcpunit.TCPServer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**zip -d electricEnergyV2.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF

 * "buildCode": "xz01",
 * "buildName": "行政楼",
 *
 * "buildCode": "kj01",
 * "buildName": "科技楼",
 *
 * "buildCode": "hdzx01",
 * "buildName": "大学生活动中心",
 *
 * "buildCode": "st01",
 * "buildName": "第一食堂",
 *
 * "buildCode": "xz02",
 * "buildName": "系办楼",
 *
 * "buildCode": "tw01",
 * "buildName": "图文中心",
 */
public class Main {
    static TCPServer tcpServer;
    private static HttpClient httpClient;

    public static void main(String[] args) {
        httpClient = new HttpClient();
        // 电能
        while (true) {
            tcpServer = new TCPServer(7001);
            tcpServer.setOnRequestListener(new TCPServer.OnRequestListener() {
                @Override
                public String onRequestListener(String request) {
                    //System.out.println("收到的客户端请求:  " + request);
                    return request;
                }
            });

            tcpServer.setOnResponseListener(new TCPServer.OnResponseListener() {
                @Override
                public String onResponseListener(String response) {
                    Double energy= Double.valueOf(0);

                    JSONObject object = JSONObject.parseObject(response);
                    Object energyObject = object.get("energy");
                    if(energyObject instanceof JSONArray){
                        JSONArray jsonArray = (JSONArray)energyObject;

                        for ( int i=0;i<jsonArray.size();i++) {
                            Double temp = Double.parseDouble(jsonArray.get(i).toString());
                            if(temp>=0){
                                energy = energy+ temp;
                            }
                        }
                    }
                    else{
                        energy = object.getDouble("energy");
                    }
                    String HTTP_URL = "https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload";
                    String dataStr = upLoadData(object.getString("pointCode") , energy);
                    System.out.println("设备端:"+response+"  上传服务器:"+dataStr);
                    httpClient.doPost(HTTP_URL, dataStr);

                    return response;
                }
            });
            tcpServer.setOnSocketChange(new TCPServer.OnSocketChange() {
                @Override
                public void onSocketChange(Set<String> devs) {
                    //System.out.print("在线客户端列表:  ");
                    for (String dev : devs) {
                        System.out.print(dev + "    ");
                    }
                    System.out.println();
                }
            });
            tcpServer.start();
            System.out.println("server start...");
            tcpServer.waitServerStop();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Service is abnormal, is restarting....");  // 服务器异常，正在重启
        }
    }

    /**
     * 组合Json
     * @param code
     * @param currentAggr
     * @return
     */
    private static String upLoadData(String code, Double currentAggr) {
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String dataStr = "{\"buildCode\":\""+code+"\",\"currentAggr\":\""+String.valueOf(currentAggr)+"\",\"energyType\":1,\"updateTime\":\""+ft.format(dNow)+"\"}";
        return dataStr;
    }
}
